package com.gitee.wsl.mathematics.geometry.d2.contour.ext

import com.gitee.wsl.mathematics.geometry.d2.contour.ShapeContour
import com.gitee.wsl.mathematics.geometry.d2.contour.contour
import com.gitee.wsl.mathematics.geometry.d2.contour.segment.Segment2D

fun ShapeContour.bulgeSegments(distortion: (index: Int, segment: Segment2D) -> Double): ShapeContour {
    val c = contour {
        moveTo(position(0.0f))
        var index = 0
        for (segment in this@bulgeSegments.segments) {
            when {
                segment.linear -> {
                    val q = segment.quadratic

                    val d = distortion(index, segment)

                    curveTo(q.control[0] + segment.normal(0.5f) * d, q.end)
                    index++
                }
                segment.control.size == 2 -> {
                    curveTo(segment.control[0], segment.control[1], segment.end)
                }
                segment.control.size == 1 -> {
                    curveTo(segment.control[0], segment.end)
                }
            }
        }
        if (this@bulgeSegments.closed) {
            close()
        }
    }
    return c
}

fun ShapeContour.bulgeSegments(distortion: Double) =
    bulgeSegments { _, _ -> distortion }

fun ShapeContour.bulgeSegments(distortion: List<Double>) =
    bulgeSegments { index, _ -> distortion[index.mod(distortion.size)] }